package Viewer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

/**
 * Panel of the nucleotide IUPAC motif search.<br>
 * It enables the search of three motifs simultaneously.<br>
 * The entered motif gets translated into a regular expression.
 * @author Stefan Lorenz<br>
 * <br>29.11.2012
 */
@SuppressWarnings("serial")
public class MotifSearch extends JPanel {

    /**
     * Viewer of the GeneScapes Session
     */
    private Viewer viewer;


    /**
     * Constructor of the motif search panel
     * @param viewer viewer of the GeneScapes session
     */
    public MotifSearch(Viewer viewer) {
	this.viewer = viewer;
	
        TitledBorder titledBorder = BorderFactory.createTitledBorder(null,
        	"IUPAC Motif Search:",
        	TitledBorder.DEFAULT_JUSTIFICATION,
        	TitledBorder.RIGHT,
        	new Font("Arial", Font.BOLD, 14),
        	Color.BLACK);
        this.setBorder(titledBorder);
	
	addSearchField();
    }

    /**
     * Adds the search fields to the Panel
     */
    private void addSearchField() {
	
	final JRadioButton showSearch = new JRadioButton("Show?");
	showSearch.setForeground(Color.BLACK);
	showSearch.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		viewer.setShowRestrictionSite(showSearch.isSelected());
	    }
	});

	final JTextField restrictionSite1 = new JTextField("");
	final JTextField restrictionSite2 = new JTextField("");
	final JTextField restrictionSite3 = new JTextField("");

	CaretListener caretListener = new CaretListener() {
	    public void caretUpdate(CaretEvent e) {
		viewer.setMotif(restrictionSite1.getText().toUpperCase(), 0);
		viewer.setMotif(restrictionSite2.getText().toUpperCase(), 1);
		viewer.setMotif(restrictionSite3.getText().toUpperCase(), 2);
	    }
	};
	restrictionSite1.addCaretListener(caretListener);
	restrictionSite2.addCaretListener(caretListener);
	restrictionSite3.addCaretListener(caretListener);
	restrictionSite1.setPreferredSize(new Dimension(80, 32));
	restrictionSite2.setPreferredSize(new Dimension(80, 32));
	restrictionSite3.setPreferredSize(new Dimension(80, 32));
	restrictionSite1.setMinimumSize(new Dimension(80, 32));
	restrictionSite2.setMinimumSize(new Dimension(80, 32));
	restrictionSite3.setMinimumSize(new Dimension(80, 32));


	final JCheckBox plus = new JCheckBox("+");
	plus.setForeground(Color.BLACK);
	plus.setSelected(this.viewer.isShowPlusSearch());
	final JCheckBox minus = new JCheckBox("-");
	minus.setForeground(Color.BLACK);
	minus.setSelected(this.viewer.isShowMinusSearch());
	ActionListener ac = new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		viewer.setShowMinusSearch(minus.isSelected());
		viewer.setShowPlusSearch(plus.isSelected());
		viewer.getDrawPanel().repaint();
	    }
	};
	plus.addActionListener(ac);
	minus.addActionListener(ac);

	this.setLayout(new GridBagLayout());
	GridBagConstraints gc = new GridBagConstraints();
	gc.gridx = 0;
	gc.gridy = 1;
	this.add(showSearch,gc);
	gc.gridx = 1;
	gc.gridy = 0;
	this.add(plus,gc);
	gc.gridx = 1;
	gc.gridy = 2;
	this.add(minus,gc);
	gc.gridx = 2;
	gc.gridy = 1;
	this.add(restrictionSite1,gc);
	gc.gridx = 3;
	this.add(restrictionSite2,gc);
	gc.gridx = 4;
	this.add(restrictionSite3,gc);
    }
}
